/*
 * #%L
 * SwiftUIDataAccessLayer
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.linkwithweb.products.swiftui.context.DBUpdateStatement;
import com.linkwithweb.products.swiftui.context.SystemContext;
import com.linkwithweb.products.swiftui.service.PlatformService;
import com.linkwithweb.products.swiftui.utils.ServiceLocator;

/**
 * @author ashwin kumar
 * 
 */
@Service("approvalProcessWatcher")
public class ApprovalProcessWatcher {

	public ApprovalProcessWatcher() {
		if (platformService == null) {
			platformService = (PlatformService) ServiceLocator.getService("platformService");
		}
	}

	private boolean isWatcherStarted = false;

	/**
	 * @return the isWatcherStarted
	 */
	public boolean isWatcherStarted() {
		return isWatcherStarted;
	}

	/**
	 * @param isWatcherStarted
	 *            the isWatcherStarted to set
	 */
	public void setWatcherStarted(boolean isWatcherStarted) {
		this.isWatcherStarted = isWatcherStarted;
	}

	@Autowired
	@Qualifier("platformService")
	private PlatformService platformService;

	/**
	 * @return the platformService
	 */
	public PlatformService getPlatformService() {
		return platformService;
	}

	/**
	 * @param platformService
	 *            the platformService to set
	 */
	public void setPlatformService(PlatformService platformService) {
		this.platformService = platformService;
	}

	public void startDirWatcher() {
		isWatcherStarted = true;
		final PlatformService platformService = this.platformService;
		TimerTask task = new DirWatcher("C:\\JDeveloper\\ChangeRequestOutputFiles\\", "txt") {
			protected void onChange(File file, String action) {
				// here we code the action on a change
				// System.out.println("File " + file.getName() + " action: " +
				// action);

				try {
					String fileContent = org.apache.commons.io.FileUtils.readFileToString(file);

					int tableNameStartIndex = fileContent.indexOf("<ns0:TableName>") + "<ns0:TableName>".length();
					int tableNameEndIndex = fileContent.indexOf("</ns0:TableName>", tableNameStartIndex);
					String tableName = fileContent.substring(tableNameStartIndex, tableNameEndIndex);

					int approvalStatusStartIndex = fileContent.indexOf("<ns0:ChangeApprovalStatus>")
							+ "<ns0:ChangeApprovalStatus>".length();
					int approvalStatusEndIndex = fileContent
							.indexOf("</ns0:ChangeApprovalStatus>", tableNameStartIndex);
					String approvalStatus = fileContent.substring(approvalStatusStartIndex, approvalStatusEndIndex);

					System.out.println(tableName);
					System.out.println(approvalStatus);

					try {
						if (StringUtils.equalsIgnoreCase(approvalStatus, "Approve")) {
							DBUpdateStatement dbUpdateStatement = (DBUpdateStatement) SystemContext.getInstance()
									.getApprovalMap().get(tableName);
							platformService.update(dbUpdateStatement.getUpdateStatement(), dbUpdateStatement
									.getDatasourceName(), dbUpdateStatement.getArguments().toArray());
							SystemContext.getInstance().getRefreshTableMap().put(tableName.toUpperCase(), tableName.toUpperCase());
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					// System.out.println(fileContent);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};

		Timer timer = new Timer();
		timer.schedule(task, new Date(), 1000);
	}
}
